var up_doc = window.dialogArguments.document;
var dataStore;
function $(name) {
	return document.getElementsByName(name);
}

function $2(name) {
	return window.dialogArguments.document.getElementsByName(name);
}

var inputMap = {
	"my:序号" : "serial",
	"my:原供应商名称" : "erp_sm",
	"my:原供应商编码" : "erp_bm",
	"my:目标供应商名称" : "to_uap_sm",
	"my:目标供应商编码" : "to_uap_bm",
	"my:目标供应商地点" : "to_site",
	"my:原供应商地点" : "from_site"
};
function repalceHTMLStr(str) {
	return !str ? "" : str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(
			/'/g, "\\'");
}
function showFormInfo(data) {
	setButtonDisabled(false);
	$('oa_number')[0].value = data.oa_number;
	$('company')[0].value = data.syb;
	var rowArray = ["<table>"];
	rowArray.push("<tr valign=\"middle\" id='showTableHead'>");
	rowArray.push("<td >选<br />择</td>");
	rowArray.push("<td >序号</td>");
	rowArray.push("<td >原供应商<br />编码</td>");
	rowArray.push("<td >原供应商名称</td>");
	rowArray.push("<td >原地点</td>");
	rowArray.push("<td >目标供应<br />商编码</td>");
	rowArray.push("<td >目标供应商名称</td>");
	rowArray.push("<td >目标地点</td>");
	rowArray.push("</tr>");
	for (var i = 0; i < data.rows.length; i++) {
		if (data.rows[i].merger) {
			rowArray.push("<tr valign='middle' class='mergerTR'>");
		} else
			rowArray.push("<tr valign='middle'>");

		rowArray.push("<td >");
		if (data.rows[i].merger) {
			rowArray.push("<input name='dataCheckbox' disabled=\"disabled\" type='checkbox'/>");
		} else
			rowArray
					.push("<input name='dataCheckbox'  type='checkbox'/>");

		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push(data.rows[i].serial);
		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push((data.rows[i].erp_bm));
		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push((data.rows[i].erp_sm));
		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push((data.rows[i].from_site));
		rowArray.push("</td>");

		rowArray.push("<td style=\"width:70px;\">");
		rowArray.push((data.rows[i].to_uap_bm));
		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push((data.rows[i].to_uap_sm));
		rowArray.push("</td>");

		rowArray.push("<td >");
		rowArray.push((data.rows[i].to_site));
		rowArray.push("</td>");
		rowArray.push("</tr>");

	}
	rowArray.push("</table>")
	document.getElementById("showMerger").innerHTML = rowArray.join("");
	dataStore = data;
}
function setButtonDisabled(disabled) {
	document.getElementById("ok_button").disabled = disabled;
}
/**
 * 将填写的内容写入到表单
 */
function okButtonClick() {
	setButtonDisabled(true);
	var dataCheckboxs = $("dataCheckbox");
	var selectCount = 0;
	for (var i = 0; i < dataCheckboxs.length; i++) {
		if (dataCheckboxs[i].checked == true) {
			selectCount++;
		}
	}
	if (selectCount == 0) {
		alert("请选择需要取消合并的项");
		setButtonDisabled(false);
		return;
	}

	$2("my:ERP供应商合并申请单单据编号")[0].value = $("oa_number")[0].value;
	$2("my:供应制造部")[0].value = $("company")[0].value;

	var formSerialArray = $2("my:序号");
	// var supplierNameNodes = $("oldSupplierName");
	var oldLength = formSerialArray.length;

	var upTR = getParentTR(formSerialArray[oldLength - 1]);
	for (var i = 0; i < selectCount; i++) {
		upTR.id = "oper";
		window.dialogArguments.addEmpty();
	}
	for (var i = 0; i < oldLength; i++) {
		getParentTR($2("my:原供应商名称")[0]).id = "oper";
		window.dialogArguments.del();
	}
	var count = 0;
	for (var i = 0; i < dataCheckboxs.length; i++) {
		if (dataCheckboxs[i].checked == true) {
			for (var key in inputMap) {
				$2(key)[count].value = dataStore.rows[i][inputMap[key]];
			}
			count++;
		}
	}
	window.close();
}
function _ajax(url, operator) {
	window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
			- 3 + "px";
	window.parent.document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				if (text == 1) {
					alert("供应商编码不能为空");
				} else if (text == 2) {
					alert("用户还未登陆，或者已掉线");
				} else if (text == 3) {
					alert("供应制造部ID不能为空。");
				} else if (text == 4) {
					alert("常数不能为空");
				} else if (text == 4) {
					alert("供应商名称不能为空");
				} else {
					var data = eval("(" + text + ")");
					if (data.success)
						operator(data);
					else
						alert(data.data);
				}
			} else {
				alert("连接服务器异常");
			}
			window.parent.document.getElementById("lodingDiv").style.display = 'none';
			setButtonDisabled(false);
		}
	};
	xmlhttp.open("get", url + "&temp=" + Math.random(), true);
	xmlhttp.send();
}

function queryFormInfo() {
	dataStore = null;
	var oa_number = $("query_oa_number")[0].value.replace(/ /g, '');
	$("query_oa_number")[0].value = oa_number;

	$('oa_number')[0].value = "";
	$('company')[0].value = "";
	// var trParentNode = $('showTableHead')[0].parentNode;
	// var childNodesLength = trParentNode.childNodes.length;
	// for (var i = childNodesLength - 1; i >= 0; i--) {
	// if ('showOANumbershowTableHeadshowCompany'
	// .indexOf(trParentNode.childNodes[i].id) == -1) {
	// trParentNode.removeChild(trParentNode.childNodes[i]);
	// }
	// }
	document.getElementById("showMerger").innerHTML = "";

	if (oa_number == "") {
		alert("ERP供应商合并申请表单据编号不能为空。");
		return;
	}
	_ajax(	"../../oa2uapController.do?method=erpMergerOperate&number="
					+ oa_number + '&operate=3', showFormInfo);
}

window.onload = function() {
	if ($2("my:ERP供应商合并申请单单据编号")[0].value
			&& $2("my:ERP供应商合并申请单单据编号")[0].value.replace(/ /g, '') != "") {
		$("query_oa_number")[0].value = $2("my:ERP供应商合并申请单单据编号")[0].value;
		queryFormInfo();
	}
};
